﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using r2mp.Classes;
using r2mp.Interface;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace r2mp.Extractor
{
    class XLS_Extractor : Interface.Extractor
    {
        private Microsoft.Office.Interop.Excel.Application xlsProj;
        private Workbook wb;
        private Worksheet ws;
        private String filePath;

        public XLS_Extractor(String filePath)
        {
            this.filePath = filePath;
            xlsProj = new Microsoft.Office.Interop.Excel.Application();
            // KOMMENTAR HIER HER!
            wb = xlsProj.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            findMTA();
            int i = countRows();
            
            //ws = wb.Worksheets.get_Item(1);
            //MessageBox.Show(ws.get_Range("A12")[1].Value2 + "");
        }

        private void findMTA()
        {
            foreach (Worksheet now in wb.Worksheets)
            {
                if ((now.get_Range("A1").Value2 + "").Equals("PSP_Code"))
                {
                    ws = now;
                    break;
                }
            }
        }

        public MTA getMTA()
        {
            findMTA();
            int maxRows = countRows();
            return null;
        }

        private int countRows()
        {
            int re = 0;
            bool end = false;
            if (ws != null)
            {
                while (!end)
                {
                    if (!(ws.get_Range("A" + re + 1).Value2 + "").Equals(""))
                    {
                        re++;
                    }
                    else
                    {
                        end = true;
                    }
                }
            }
            else
            {

            }
            // -1 wegen Titelzeile
            return re - 1;
        }

        public Task getTasks()
        {
            throw new NotImplementedException();
        }
    }
}
